package FrameWork.Skill;

import FrameWork.Model.GFighter;
import FrameWork.Model.Player;

import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class SkillFacade {

    public ScheduledExecutorService scheduledExecutorService;

    public void useSkill(int skillId,GFighter effector,GFighter target,long castTime){
        SkillModel skill = SkillManager.getSkillModel(skillId);
        if (consumeCost(skill,effector)){
            //返回前端已经准备好进行前绕动作
            //更新CD
            updateCD(skill,effector);
            long delay = castTime+skill.getDelay() - System.currentTimeMillis();
            if (delay<0) delay =0;
            Future future = scheduledExecutorService.schedule(()->{
                if (skill.getBeforeExecuteSkills()!=null){
                    for (int preSkillId:skill.getBeforeExecuteSkills()){
                        SkillModel model = SkillManager.getSkillModel(preSkillId);
                        useSkill0(model,effector,target);
                    }
                    useSkill0(skill,effector,target);
                    if (skill.getAfterExecuteSkills()!=null){
                        for (int afterId:skill.getAfterExecuteSkills()){
                            SkillModel model = SkillManager.getSkillModel(afterId);
                            useSkill0(model,effector,target);
                        }
                    }
                }
            },delay,TimeUnit.MILLISECONDS);
            effector.setCastingSkill(future);
        }
    }

    /**
     * TODO 这里要有一个合并效果packet
     * @param skillModel
     * @param effector
     * @param target
     */
    private void useSkill0(SkillModel skillModel,GFighter effector,GFighter target){

    }

    private void updateCD(SkillModel skillModel,GFighter effetor){

    }

    private boolean consumeCost(SkillModel model,GFighter effector){
        //检查条件触发
        effector.setHp(effector.getHp() + model.getCostHp());
        effector.setMp(effector.getHp() + model.getCostHp());
        //同步到前端
        return true;
    }

    private void effectTarget(SkillModel model,GFighter target){

    }

}
